
import com.etop.weibo.beans.Praise;
import com.etop.weibo.dao.DAOFactory;
import com.etop.weibo.dao.PraiseDao;
import com.etop.weibo.util.HibernateUtil;
import java.util.List;
import org.hibernate.Session;
import org.junit.Test;

/**
 *
 * @author jansing
 */
public class PraiseCRUD {
    
    public void say01(){
        Session session = HibernateUtil.openSession();
//        List<Object[]> list = session.createQuery("select count(*) as c,p.sayId from Praise p group by p.sayId").list();
//        List<Object[]> list = session.createQuery("select count(p.sayId),p.sayId from Say s left join Praise as p where p.sayId=s.id group by p.sayId").list();

        //sql 语句执行成功
        List<Object[]> list = session.createSQLQuery("select s.id, t.c from"
                + " t_say s"
                + " left join (select count(p.s_id) as c,p.s_id from t_praise p group by p.s_id) t"
                + " on s.id=t.s_id")
                .list();

        //hql 不行，可能需要在Say中新增praise对象
        List<Object[]> list1 = session.createQuery("select s.id, t.c from"
                + " Say s"
                + " left join (select count(p.sayId) as c,p.sayId as s_id from Praise p group by p.sayId) t"
                + " on s.id=s_id")
                .list();
        for(Object[] o: list){
            System.out.println(o[0]+" -- " + o[1]);
        }
    }
    
    public void load01(){
        System.out.println(DAOFactory.getPraiseDao().load(44, 44, 44));
        
    }
    
    public void count01(){
        System.out.println(DAOFactory.getPraiseDao().count(-1, 1));
    }
    
    @Test
    public void delete01(){
//        new PraiseDao().testDelete(5, 6, 0);
//        DAOFactory.getPraiseDao().delete(1,3,1);
    }
    
    public void add01(){
        Praise p = new Praise();
        p.setCommentId(-1);
        p.setSayId(3);
        p.setUserId(4);
        DAOFactory.getPraiseDao().add(p);
    }
    
}
